// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


#ifndef OpenMeca_Item_LinkBase_hpp
#define OpenMeca_Item_LinkBase_hpp

#include "OpenMeca/Core/UserRootItem.hpp"
#include "OpenMeca/Core/AutoRegisteredPtr.hpp"
#include "OpenMeca/Gui/UserItemDialogNone.hpp"
#include "OpenMeca/Gui/SecondaryTreeItem.hpp"
#include "OpenMeca/Util/Color.hpp"
#include "OpenMeca/Core/None.hpp"
#include "OpenMeca/Item/Body.hpp"
#include "OpenMeca/Geom/Frame.hpp"
#include "OpenMeca/Geom/Point.hpp"
#include "OpenMeca/Geom/Quaternion.hpp"


namespace OpenMeca
{  
  namespace Item
  {

    
    struct None
    {
    };

    

    class Body;

    class LinkBase: public Core::UserRootItem , public Core::AutoRegister<LinkBase>
    {
    
    public:
      static const std::string GetStrType(); 
      static void Init();

      typedef Core::None Dialog;

      static void DrawIcon(QIcon&, QColor, QColor);
      

    public:
      LinkBase(const std::string&);
      virtual ~LinkBase();

      Body& GetBody1();
      Body& GetBody2();

      const Body& GetBody1() const;
      const Body& GetBody2() const;

      Core::AutoRegisteredPtr<Body, LinkBase>& GetBody1Ptr();
      Core::AutoRegisteredPtr<Body, LinkBase>& GetBody2Ptr();

      const Geom::Point<_3D>& GetCenter() const;
      Geom::Point<_3D>& GetCenter();
      
      const Geom::Quaternion<_3D>& GetQuaternion() const;
      Geom::Quaternion<_3D>& GetQuaternion();


      void Update();

      template<int N> Body& GetBody();
      template<int N> const Body& GetBody() const;
      


    private:
      LinkBase();                        //Not allowed
      LinkBase(const LinkBase&);             //Not Allowed
      LinkBase& operator=(const LinkBase&);  //Not Allowed

      void EraseBody1();
      void EraseBody2();

      friend class boost::serialization::access;
      template<class Archive>
      void serialize(Archive & ar, const unsigned int);

    private:
      Core::AutoRegisteredPtr<Body, LinkBase> body1_;
      Core::AutoRegisteredPtr<Body, LinkBase> body2_;
      // Gui::SecondaryTreeItem treeItembody1_;
      // Gui::SecondaryTreeItem treeItembody2_;
      Geom::Point<_3D> center_;
      Geom::Quaternion<_3D> quaternion_;
      Geom::Frame<_3D> frame_;      
    };

    template<class Archive>
    inline void
    LinkBase::serialize(Archive & ar, const unsigned int)
    {
      ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Core::UserRootItem);
      ar & BOOST_SERIALIZATION_NVP(body1_);
      ar & BOOST_SERIALIZATION_NVP(body2_);
      ar & BOOST_SERIALIZATION_NVP(center_);
      ar & BOOST_SERIALIZATION_NVP(quaternion_);
      //ar & BOOST_SERIALIZATION_NVP(frame_);
    }


    template<int N> 
    inline Body&  
    LinkBase::GetBody()
    {
      assert(0);
    }


    template<int N> 
    inline const Body& 
    LinkBase::GetBody() const
    {
      assert(0);
    }


  }

}




#endif
